package com.arsn.myf.common.util.node;

import com.arsn.myf.common.node.MenuTreeNode;
import com.arsn.myf.common.node.TreeNode;
import com.arsn.myf.system.entity.Menu;
import com.arsn.myf.system.vo.MenuVO;
import com.arsn.myf.system.vo.RoleVO;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * node tree 工具类
 * @author zhongrj
 * @since 2021-11-23
 */
public class NodeTreeUtil {

    /**
     * map 转 tree
     * @param map
     * @return
     */
    public static List<TreeNode> getNodeTree(Map<Long, TreeNode> map){
        List<TreeNode> tree = new ArrayList<>();
        if (map.size() > 1) {
            map.forEach((id, treeNode) -> {
                if (map.containsKey(treeNode.getParentId())) {
                    map.get(treeNode.getParentId()).getChildren().add(treeNode);
                } else {
                    tree.add(treeNode);
                }
            });
        }
        return tree;
    }

    /**
     * map 转 tree 组织机构
     * @param map
     * @return
     */
    public static List<RoleVO> getRoleNodeTree(Map<Long, RoleVO> map){
        List<RoleVO> tree = new ArrayList<>();
        if (map.size() > 1) {
            map.forEach((id, treeNode) -> {
                if (map.containsKey(treeNode.getParentId())) {
                    map.get(treeNode.getParentId()).getChildren().add(treeNode);
                } else {
                    tree.add(treeNode);
                }
            });
        }
        return tree;
    }

    /**
     * map 转 tree 角色
     * @param map
     * @return
     */
    public static List<MenuVO> getMenuNodeTree(Map<Long, MenuVO> map){
        List<MenuVO> tree = new ArrayList<>();
        if (map.size() >= 1) {
            List<MenuVO> finalTree = tree;
            map.forEach((id, treeNode) -> {
                if (map.containsKey(treeNode.getParentId())) {
                    map.get(treeNode.getParentId()).getChildren().add(treeNode);
                } else {
                    finalTree.add(treeNode);
                }
            });
            //排序
            tree = finalTree.stream().sorted(Comparator.comparing(MenuVO::getSort)).collect(Collectors.toList());
            //遍历
            for (MenuVO treeNode : finalTree) {
                if (treeNode.getChildren().size()>0){
                    List<Menu> childrenTree = treeNode.getChildren();
                    //排序
                    treeNode.setChildren(
                            childrenTree.stream().sorted(Comparator.comparing(Menu::getSort)).collect(Collectors.toList())
                    );
                }
            }
        }
        return tree;
    }


    /**
     * map 转 tree  系统tree 菜单
     * @param map
     * @return
     */
    public static List<MenuTreeNode> getSystemNodeTree(Map<Long, MenuTreeNode> map){
        List<MenuTreeNode> tree = new ArrayList<>();
        if (map.size() > 1) {
            List<MenuTreeNode> finalTree = tree;
            map.forEach((id, treeNode) -> {
                if (map.containsKey(treeNode.getParentId())) {
                    map.get(treeNode.getParentId()).getChildren().add(treeNode);
                } else {
                    finalTree.add(treeNode);
                }
            });
            //排序
            tree = finalTree.stream().sorted(Comparator.comparing(MenuTreeNode::getSort)).collect(Collectors.toList());
            //遍历
            for (MenuTreeNode treeNode : finalTree) {
                if (treeNode.getChildren().size()>0){
                    List<MenuTreeNode> childrenTree = treeNode.getChildren();
                    //排序
                    treeNode.setChildren(
                       childrenTree.stream().sorted(Comparator.comparing(MenuTreeNode::getSort)).collect(Collectors.toList())
                    );
                }
            }
        }
        return tree;
    }
}
